package com.yuejian_06.yuejian_06.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.yuejian_06.yuejian_06.Listener.MeListener;
import com.yuejian_06.yuejian_06.R;
import com.yuejian_06.yuejian_06.adapter.PicInfoAdapter;
import com.yuejian_06.yuejian_06.adapter.PostAdapter;
import com.yuejian_06.yuejian_06.bean.CommentBean;
import com.yuejian_06.yuejian_06.bean.PicBean;
import com.yuejian_06.yuejian_06.bean.PostListBean;
import com.yuejian_06.yuejian_06.bean.SuccessBean;
import com.yuejian_06.yuejian_06.bean.UserInfoBean;
import com.yuejian_06.yuejian_06.model.MeModel;
import com.yuejian_06.yuejian_06.model.PicModel;

import java.util.List;

import static com.yuejian_06.yuejian_06.R.id.comment;

public class PicInfoActivity extends BaseActivity {
    private TextView user_name,club_name,pic_main,createtime;
    private ImageView user_pic,pic1,pic2,pic3;
    private EditText et_comment;
    private Button comment;
    private String pic_id;;
    private ImageButton back;

    private RecyclerView recyclerView;
    private SwipeRefreshLayout activity_pic_info;

    @Override
    void initViews() {

    }

    @Override
    void initEvents() {

    }

    @Override
    void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic_info);
        initView();//初始化事件
        initEvent();
        Intent intent = getIntent();
        pic_id = intent.getStringExtra("pic_id");
        //System.out.println("----"+pic_id+"----");
        PicModel picModel = new PicModel();
        picModel.getPicInfo(pic_id,meListener);
        recyclerView = (RecyclerView) findViewById(R.id.commentlist_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(PicInfoActivity.this));
        PicModel picModel1 = new PicModel();
        picModel1.getCommentList(pic_id,meListener1);

        //下拉刷新
        activity_pic_info = (SwipeRefreshLayout) findViewById(R.id.activity_pic_info);
        activity_pic_info.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override public void onRefresh() {
                //重新获取数据
                PicModel picModel1 = new PicModel();
                picModel1.getCommentList(pic_id,comremeListener);
                //获取完成
                activity_pic_info.setRefreshing(false);
            }
        });
    }

    private void initView(){
        user_name = (TextView) findViewById(R.id.user_name);
        club_name = (TextView) findViewById(R.id.club_name);
        pic_main = (TextView) findViewById(R.id.pic_main);
        createtime = (TextView) findViewById(R.id.createtime);
        user_pic = (ImageView) findViewById(R.id.user_pic);
        pic1 = (ImageView) findViewById(R.id.pic1);
        pic2 = (ImageView) findViewById(R.id.pic2);
        pic3 = (ImageView) findViewById(R.id.pic3);

        et_comment = (EditText) findViewById(R.id.et_comment);
        comment = (Button) findViewById(R.id.comment);
        back = (ImageButton) findViewById(R.id.back);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private MeListener<PicBean> meListener =new MeListener<PicBean>() {
        @Override
        public void onResponse(PicBean o) {
            PicBean res=o;
            user_name.setText(res.getUser_name());
            club_name.setText("#"+res.getClub_name()+"#");
            pic_main.setText(res.getPic_main());
            createtime.setText(res.getCreatetime());
            if (!"".equals(res.getUser_pic()))
                Picasso.with(PicInfoActivity.this).load("http://10.0.2.2/yuejian/Public/picpics/"+res.getUser_pic()).into(user_pic);
            if (!"".equals(res.getPic1()))
                Picasso.with(PicInfoActivity.this).load("http://10.0.2.2/yuejian/Public/picpics/"+res.getPic1()).into(pic1);
            if (!"".equals(res.getPic2()))
                Picasso.with(PicInfoActivity.this).load("http://10.0.2.2/yuejian/Public/picpics/"+res.getPic2()).into(pic2);
            if (!"".equals(res.getPic3()))
                Picasso.with(PicInfoActivity.this).load("http://10.0.2.2/yuejian/Public/picpics/"+res.getPic3()).into(pic3);
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(PicInfoActivity.this,"详情失败",Toast.LENGTH_SHORT).show();
        }
    };

    private MeListener<List<CommentBean>> meListener1 =new MeListener<List<CommentBean>>() {
        @Override
        public void onResponse(List<CommentBean> o) {
            List<CommentBean> res1=o;
            List<CommentBean> mDataList1 =(List) res1;
            recyclerView.setAdapter(new PicInfoAdapter(PicInfoActivity.this,mDataList1));
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(PicInfoActivity.this,"帖子列表失败",Toast.LENGTH_SHORT).show();
        }
    };

    //刷新
    private MeListener<List<CommentBean>> comremeListener =new MeListener<List<CommentBean>>() {
        @Override
        public void onResponse(List<CommentBean> o) {
            List<CommentBean> res1=o;
            List<CommentBean> mDataList1 =(List) res1;
            recyclerView.setAdapter(new PicInfoAdapter(PicInfoActivity.this,mDataList1));
            Toast.makeText(PicInfoActivity.this,"留言刷新成功",Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(PicInfoActivity.this,"留言刷新失败",Toast.LENGTH_SHORT).show();
        }
    };




    private void initEvent(){
        comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                AlertDialog.Builder builder = new AlertDialog.Builder(PicInfoActivity.this);
                builder.setIcon(R.drawable.heart_p);
                builder.setTitle(R.string.del);
                builder.setMessage(R.string.postcomment);
                builder.setPositiveButton(R.string.postive_button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String comment_content = et_comment.getText().toString();
                        PicModel picModel = new PicModel();
                        picModel.getPostComment(getUser_id(),pic_id,comment_content,meListener2);
                    }
                });

                builder.setNegativeButton(R.string.negative_button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(PicInfoActivity.this,"已经取消",Toast.LENGTH_SHORT).show();
                    }
                });

                builder.setCancelable(false);
                AlertDialog dialog=builder.create();
                dialog.show();
            }
        });
    }

    private MeListener<SuccessBean> meListener2 =new MeListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean o) {
            Toast.makeText(PicInfoActivity.this,"发表留言成功",Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(PicInfoActivity.this,"发表留言失败",Toast.LENGTH_SHORT).show();
        }
    };


}
